//
//  HYShortViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYShortViewController.h"
#import "HYShortTableViewCell.h"
#import <MJExtension.h>
#import "PKRequestManager.h"
#import "CJPictorialCellWebViewViewController.h"
#import "HYMainMoviePlagerViewController.h"
#import "UMSocial.h"

@interface HYShortViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;


@end
static NSString *identifier = @"HYShortViewController";
@implementation HYShortViewController

//短片
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableArray = [NSMutableArray array];

    self.view.backgroundColor = kRandomClolor;
    [self setUpData];
    
    
    // Do any additional setup after loading the view.
}

- (void) setUpData {
    
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=threadvideo&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        NSArray *listArray = dataDic[@"data"][@"list"];
                for (NSDictionary *dic in listArray) {
            _model = [[HYMoreShortModel alloc] init];
            [_model setValuesForKeysWithDictionary:dic];
            [_tableArray addObject:_model];
        }
        
        
        [self addTableView];
        
        
    } error:^(NSError *error) {
        
    }];

}
- (void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 113) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HYShortTableViewCell class] forCellReuseIdentifier:identifier];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HYShortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = _tableArray[indexPath.row];
    
    [cell.shortImageButton addTarget:self action:@selector(shortImageButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [cell.bottomView.praiseButton addTarget:self action:@selector(praiseButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [cell.bottomView.moreButton addTarget:self action:@selector(moreButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = [HYShortTableViewCell labelAndCellHeight:_tableArray[indexPath.row]];
   
    return height + kScreenHeight /3 + 120;
    
}

- (void)shortImageButton:(UIButton *)button {
    HYMainMoviePlagerViewController *movieVc = [[HYMainMoviePlagerViewController alloc] init];
    movieVc.movie = _model.videourl;
    [self.navigationController pushViewController:movieVc animated:YES];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJPictorialCellWebViewViewController *webView = [[CJPictorialCellWebViewViewController alloc] init];
    HYMoreShortModel *model = _tableArray[indexPath.row];
    webView.tid = model.tid;
    [self.navigationController pushViewController:webView animated:YES];
}

//点赞
- (void)praiseButton:(UIButton *)button {
    
    
    // 是否登录userName isEqualToString:@"YES"]
    if ([SZLoginTool shareLogin].isLogin) {
        
        button.selected = !button.selected;
        
        NSString *isLike = nil;
        if (button.selected) {
            isLike = @"like";
        } else {
            isLike = @"unlike";
        }
        
        
        //NSArray *listArray = self.model.list;
       // HYMoreShortModel *model = listArray[button.tag - 11000];
        
        NSString *str = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=threadlikecp&op=%@&tid=%@&formhash=%@&androidflag=1&appfrom=ios&iosversion=3.13",isLike,_model.tid, [SZLoginTool shareLogin].formhash];
        
        [PKRequestManager requestWithUrlString:str parDic:nil method:GET finish:^(NSDictionary *dataDic) {
            if (dataDic[@"data"][@"result"]) {
                SZLog(@"点赞成功");
                
                _model.isliked = button.selected;
            } else {
                SZLog(@"点赞失败");
            }
            
            
        } error:^(NSError *error) {
            
        }];
        
    } else {
        
        SZLoginViewController *loginVC = [[SZLoginViewController alloc] init];
        UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNav animated:YES completion:nil];
        
        
    }
}

//分享
- (void)moreButton:(UIButton *)button {
   
//    NSArray *listArray = self.model.list;
//    CJShadowMovieListModel *model = listArray[button.tag - 12000];
    
    NSString *url = [NSString stringWithFormat:@"http://morguo.com/forum.php?mod=viewthread&tid=%@&isfrommobile=1",_model.tid];
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *viewController = window.rootViewController;
    [UMSocialSnsService presentSnsIconSheetView:viewController
                                         appKey:@"569ee21fe0f55a76b10021e3"
                                      shareText:[NSString stringWithFormat:@"<<%@>>详情点击%@",_model.subject,url]
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToEmail,UMShareToWechatFavorite,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToTencent,nil]
                                       delegate:nil];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
